find_program(GETTEXT_MSGFMT_EXECUTABLE msgfmt REQUIRED)

file(GLOB PO_FILES "*.po")

set(MO_FILES)
foreach(PO_FILE ${PO_FILES})
    get_filename_component(LANG_CODE ${PO_FILE} NAME_WE)
    
    set(MO_FILE ${CMAKE_CURRENT_BINARY_DIR}/${LANG_CODE}.mo)
    set(INSTALL_PATH ${PACKAGE_LOCALE_DIR}/${LANG_CODE}/LC_MESSAGES/${PROJECT_NAME}.mo)

    add_custom_command(
        OUTPUT ${MO_FILE}
        COMMAND ${GETTEXT_MSGFMT_EXECUTABLE} -o ${MO_FILE} ${PO_FILE}
        DEPENDS ${PO_FILE}
        WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
        COMMENT "Compiling ${PO_FILE} to ${MO_FILE}"
    )

    list(APPEND MO_FILES ${MO_FILE})

    install(FILES ${MO_FILE}
            DESTINATION ${PACKAGE_LOCALE_DIR}/${LANG_CODE}/LC_MESSAGES
            RENAME ${PROJECT_NAME}.mo
    )
endforeach()

# 添加自定义目标以生成所有 MO 文件
add_custom_target(generate_mo_files ALL DEPENDS ${MO_FILES})
